package service.impl;

import Dao.BaseDao;
import Dao.impl.BaseDaoImpl;
import entity.Orders;
import entity.Train;
import entity.User;
import service.BaseService;
import service.BuyTicketsService;
import service.UserService;
import untils.Msg;
import untils.SqlCollection;

import java.util.ArrayList;
import java.util.Scanner;

public class BaseServiceImpl implements BaseService {

    private BaseDao baseDao = new BaseDaoImpl();
    private SqlCollection sqlCollection = new SqlCollection();
    static Scanner sc = new Scanner(System.in);
    private static BuyTicketsService buyTicketsService = new BuyTicketsServiceImpl();
    private UserService userService = new UserServiceImpl();
    @Override
    public Msg<User> Login(String phone, String pwd) {
        //1.返回登录结果
        //2.通过Dao层获取用户信息进行判断
        Msg<User> msg = baseDao.login(phone);
        if(msg.getData()!=null){
            if(msg.getData().getPassword().equals(pwd)){
                 msg.setMsg("SUCCEED");
            }else {
                msg.setMsg("PWD_ERROR");
            }
        }else {
            msg.setMsg("USERNAME_ERROR");
        }
        return msg;
    }

    @Override
    public Msg<User> register(User user) {

        Msg<User> msg = new Msg<>();
        try{
            sqlCollection.beginTransaction();
            //1.判断添加用户是否已经注册 :电话号码信息
            int flag = baseDao.findUserByPhone(user.getPhone());

            if(flag>0){
                System.out.println("您输入的账号已经被注册，请你检查相关信息");
            }else {
                msg = baseDao.register(user);
            }
            //成功：事务提交
            sqlCollection.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlCollection.rollback();
        }finally {
            sqlCollection.closeConnection();
        }
        //开启事务
        return msg;
    }



    @Override
    public Msg<User> updatePersonalData(Integer  id, String username,String password,String real_name,String phone,String email) {
        return baseDao. updatePersonalData(id,username,password,real_name,phone,email);
    }

    @Override
    public Msg<ArrayList<Train>> findAllTrainData() {
        return baseDao.findAllTrainData();
    }

    /**
     * 查询车票信息
     *
     * @return 返回包含查询结果的车票信息列表
     */
    //查询车票信息
    public ArrayList<Train> FindTicketsMessage() {
        System.out.println("请输入你的乘坐区间");
        System.out.println("请输入开始地址");
        String startStation = sc.next();
        System.out.println("请输入终点");
        String endStation = sc.next();
        System.out.println("请输入乘车时间");
        String time = sc.next();
        ArrayList<Train> tickets = buyTicketsService.findTickesData(startStation, endStation, time ,2);
        int count = 0;
        for (Train train : tickets) {
            System.out.println(count + "\t" + train);
            count++;
        }
        return tickets;
    }

    public Train selectTrain(){
        ArrayList<Train> trains = FindTicketsMessage();
        System.out.println("请选择你想购买的票：");
        System.out.println("输入选择列表：");
        int listId = sc.nextInt();
        Train train = trains.get(listId);
        //判断购票时间为车出发前三个小时，否则提示即将发车购票系统已经关闭，请您选择其他时段的票

        //？？？？？？？？
        return train;
    }
    public ArrayList<User> SelectUser(int userId){

         System.out.println("请选择乘车人信息：");
         ArrayList<User> users = userService.findUserData(userId);
         for (int i = 0;i<users.size();i++){
             System.out.println(i + "\t" + users.get(i).show());
         }
         if (users == null || users.size() == 0) {
             System.out.println("您还没有添加乘车人信息，请您添加");
         }
         System.out.println("是否添加用户信息（y/n）");
         String add = sc.next();
         if ("y".equals(add)) {
             System.out.println("1.添加用户");
             while (true) {
                 System.out.println("请输入真实姓名：");
                 String uName = sc.next();
                 System.out.println("请输入电话号码：");
                 String otherphone = sc.next();
                 System.out.println("身份证号：");
                 String idCard = sc.next();
                 userService.addUserData(userId, uName, otherphone, idCard);
                 System.out.println("是否继续添加：（y/n）");
                 String select = sc.next();
                 if ("y".equals(select)) {
                     userService.addUserData(userId, uName, otherphone, idCard);
                     System.out.println("是否继续添加（y/n）");
                     select = sc.next();
                 } else {
                     break;
                 }
             }
         }
         System.out.println("选择乘坐人");
         ArrayList<User> SelectUser = new ArrayList<>();
         String s = "y";
         while ("y".equals(s)) {
             System.out.println("请输入你要选择的用户ListID");
             int ListId = sc.nextInt();
             if(ListId>users.size()-1){
                 System.out.println("您输入的用户不存在请重新输入");
             }else {
                 SelectUser.add(users.get(ListId));
             }
             System.out.println("是否继续加入(y/n)");
             String s1 = sc.next();
             s = s1;
         }
         return SelectUser;
     }



}
